﻿using System;
using LibQuestionIsPhone.Models;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Net.NetworkInformation;

namespace InterviewAppPhone.Views
{
    public partial class Difficolta : PhoneApplicationPage
    {
        public Difficolta()
        {
            InitializeComponent();
            if (!App.DifficoltaViewModel.IsDataLoaded)
                App.DifficoltaViewModel.LoadData();
            DataContext = App.DifficoltaViewModel;
            App.DifficoltaViewModel.IsConnection = DeviceNetworkInformation.IsNetworkAvailable;
            DeviceNetworkInformation.NetworkAvailabilityChanged += DeviceNetworkInformation_NetworkAvailabilityChanged;
        }

        void DeviceNetworkInformation_NetworkAvailabilityChanged(object sender, NetworkNotificationEventArgs e)
        {
            switch (e.NotificationType)
            {
                case NetworkNotificationType.CharacteristicUpdate:
                    break;
                case NetworkNotificationType.InterfaceConnected:
                    Dispatcher.BeginInvoke(() =>
                    { App.DifficoltaViewModel.IsConnection = true; }
                    );
                    break;
                case NetworkNotificationType.InterfaceDisconnected:
                    Dispatcher.BeginInvoke(() =>
                    { App.DifficoltaViewModel.IsConnection = false; }
                    );
                    break;
                default:
                    break;
            }
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            App.DomandeViewModel = null;
            App.DifficoltaViewModel.IsConnection = DeviceNetworkInformation.IsNetworkAvailable;
            base.OnNavigatedTo(e);
        }

        private void MainLongListSelector_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            // Se la voce selezionata è nulla (nessuna selezione) non eseguire alcuna azione
            if (MainLongListSelector.SelectedItem == null)
                return;

            DataFlowObject.Difficolta = (MainLongListSelector.SelectedItem as LibQuestionIsPhone.Models.Difficolta);

            NavigationService.Navigate(new Uri("/Views/Domande.xaml", UriKind.Relative));

            // Reimposta la voce selezionata su null (nessuna selezione)
            MainLongListSelector.SelectedItem = null;
        }
    }
}